package com.shenhaofeng.app.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shenhaofeng.app.R;


/**
 * Created by Administrator on 2016/7/22.
 */
public abstract class MoreAdapter<H> extends HeaderFooterAdapter<Status, H> {


    private Status mStatus;


    public MoreAdapter() {
        mStatus = new Status(super.getFooterModule());
        mStatus.put(Status.STATUS_MORE, "上拉加载更多");
        mStatus.put(Status.STATUS_NOT_MORE, "加载完毕");
        mStatus.put(Status.STATUS_MORE_LOADING, "正在加载中");
        mStatus.put(Status.STATUS_MORE_ERROR, "加载失败");
        mStatus.setCurrentStatus(Status.STATUS_MORE);
    }


    /**
     * 装载状态项
     */
    public void installStatus() {
        if (!super.getFooterModule().getItemList().contains(mStatus)) {
            super.getFooterModule().addItem(mStatus);
        }
    }


    /**
     * 卸载状态Item
     */
    public void uninstallStatus() {
        if (super.getFooterModule().getCount() != 0) {
            super.getFooterModule().clear();
        }
    }


    @Override
    protected RecyclerView.ViewHolder onCreateFootViewHolder(ViewGroup parent) {
        return onCreateMoreViewHolder(parent);
    }


    /**
     * 在创建More 的itemViewHolder时将会被调用，重写该方法可以自定义状态页脚，数据绑定重写{@code #onBindFootViewHolder}
     *
     * @param parent
     * @return
     */
    protected MoreViewHolder onCreateMoreViewHolder(ViewGroup parent) {
        return new MoreViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.more, parent, false));
    }


    /**
     * 该方法被精致调用
     *
     * @return
     * @deprecated
     */
    @Override
    public ModuleAdapter.Module<Status> getFooterModule() {
        throw new UnsupportedOperationException("禁止获取");
    }


    /**
     * 如果需要绑定页脚数据，使用{@code #onBindMoreViewHolder}
     *
     * @param holder
     * @param position
     */
    @Deprecated
    @Override
    protected void onBindFootViewHolder(RecyclerView.ViewHolder holder, int position) {
        onBindMoreViewHolder(holder, mStatus);
    }


    /**
     * 绑定页脚时被调用,需要自定义页脚状态可以重写该方法
     *
     * @param holder
     * @param mStatus
     */
    protected void onBindMoreViewHolder(RecyclerView.ViewHolder holder, Status mStatus) {
        MoreViewHolder moreViewHolder = (MoreViewHolder) holder;
        moreViewHolder.moreMessageTv.setText(mStatus.getCurrentMessage());
    }


    /**
     * 获取状态实例
     *
     * @return
     */
    public Status getStatus() {
        return mStatus;
    }


    /**
     * 内部调用页脚module的方法
     *
     * @return
     */
    protected ModuleAdapter.Module<Status> _getFooterModule() {
        return super.getFooterModule();
    }


    /**
     * 默认的More ViewHolder对象
     */
    public static class MoreViewHolder extends RecyclerView.ViewHolder {


        public TextView moreMessageTv;


        public MoreViewHolder(View itemView) {
            super(itemView);
            moreMessageTv = (TextView) itemView.findViewById(R.id.message_tv);
        }
    }
}
